from Mysqldb import Connection
from dyrequests import DyRequests
import traceback
import emoji
import time
import requests

def get_video_info(uid):
    s = Connection()
    sql = 'select * from dy_author_id where uid=%s'
    data = [uid]
    ss = s.get(sql,data)
    if ss is not None:
        sec_id = ss.get('sec_id')
        max_cursor = 0
        info_list = []
        count = 0
        get_res(sec_id,max_cursor,info_list,count)
        return info_list

def get_res(sec_id,max_cursor,info_list,count=0):
    device = 'vivo%21X21'
    user_id = ''
    url = 'https://api3-normal-c-lq.amemv.com/aweme/v1/forward/list/?user_id={user_id}&sec_user_id={sec_id}&max_cursor={max_cursor}&min_cursor=0&count=20&os_api=22&device_type={device}&ssmix=a&manifest_version_code=110301&dpi=320&uuid=863064420217327&app_name=aweme&version_name=11.3.0&ts=1592270766&cpu_support64=false&app_type=normal&ac=wifi&host_abi=armeabi-v7a&update_version_code=11309900&channel=tengxun_new&_rticket=1592270766966&device_platform=android&iid=2506536184985592&version_code=110300&mac_address=74%3AD4%3A35%3AD2%3AAD%3AD1&cdid=c250afeb-494e-4009-b2f3-ac5dff5d81de&openudid=4b344aa37097205a&device_id=1028792555420824&resolution=1600*900&os_version=5.1.1&language=zh&device_brand=huawei&aid=1128&mcc_mnc=46007'.format(
            user_id=user_id, sec_id=sec_id, max_cursor=max_cursor, device=device)
    res = DyRequests().request(url)
    try:
        data = res.json()
        max_cursor = data.get("max_cursor")
        if data.get("dongtai_list") is None:
            return
        for item in data.get("dongtai_list"):
            item = item.get("aweme")
            digg_count = item.get("statistics").get("digg_count")
            download_count = item.get("statistics").get("download_count")
            comment_count = item.get("statistics").get("comment_count")
            share_count = item.get("statistics").get("share_count")
            forward_count = item.get("statistics").get("forward_count")
            short_id = item['author']['short_id']  # 抖音号
            uid = item['author']['uid']  # 抖音id
            nickname = item['author']['nickname']  # 抖音id
            duration = int(item.get("video").get("duration")) / 1000
            try:
                print(item.get("video"))
                video_cover = item.get("video").get("origin_cover").get("url_list")[0]
            except:
                print('ssss')
                continue
            share_url = item.get("share_url")
            aweme_id = item.get("aweme_id")
            music_id = item.get("music").get("id_str")
            share_title = item.get("share_info").get("share_title")
            create_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(item.get("create_time")))
            text_extra = item.get("text_extra")
            # print('text_extra-------',text_extra)
            tags = []
            for text in text_extra:
                hashtag_name = text.get("hashtag_name")
                if hashtag_name is not None:
                    tags.append(hashtag_name)
            tags = ','.join(tags)
            # print(share_title,create_time)
            info = {}
            info["digg_count"] = int(digg_count)
            info["short_id"] = int(uid)
            info["nickname"] = emoji.demojize(nickname)
            info["tags"] = emoji.demojize(tags)
            info["digg_count"] = int(digg_count)
            info["create_time"] = create_time
            info["download_count"] = int(download_count)
            info["comment_count"] = int(comment_count)
            info["forward_count"] = int(forward_count)
            info["share_count"] = int(share_count)
            info["duration"] = duration
            info["video_cover"] = video_cover
            info["share_url"] = share_url
            info["share_title"] = emoji.demojize(share_title)
            info["music_id"] = music_id
            info["aweme_id"] = aweme_id
            info["itemname"] = 'video'
            info_list.append(info)
            if len(info)>50:
                return
            # print('yide info')
        if data.get("dongtai_list") is not None:
            count+=1
            get_res(sec_id,max_cursor,info_list)
    except Exception as e:
        traceback.print_exc()


def get_user_info(uid):
    s = Connection()
    sql = 'select * from dy_author_id where uid=%s'
    data = [uid]
    ss = s.get(sql, data)
    if ss is not None:
        sec_id = ss.get('sec_id')
        age = ss.get("age")
        region = ss.get("region")
        synopsis = ss.get("synopsis")
        sex = ss.get("sex")
        info_list = []
        url = 'https://www.iesdouyin.com/web/api/v2/user/info/?sec_uid={}'.format(sec_id)
        resp = requests.get(url=url).json()
        info = resp.get("user_info")
        if 1:
            temp = {}
            temp['author_name'] = emoji.demojize(info['nickname'])  # 作者名字
            temp['author_img'] = info['avatar_thumb']['url_list'][0]  # 作者头像
            temp['works'] = info['aweme_count']  # 作品数
            temp['fans_number'] = info['follower_count']  # 粉丝数
            temp['like_number'] = info['total_favorited']  # 点赞数
            if info['unique_id'] == None or info['unique_id'] == "":
                temp['tik_tok'] = info['short_id']  # 抖音号
            else:
                temp['tik_tok'] = info['unique_id']
            temp['author_id'] = info['uid']
            temp['age'] = age
            temp['region'] = region
            temp['synopsis'] = synopsis
            temp['sex'] = sex
            temp['class_name'] = 1
            return temp

# get_video_info()
if __name__ == "__main__":
    max_cursor = 0
    info_list = []
    count = 0
    sec_id = 'MS4wLjABAAAAUSx19G2hyoT1P6VTvDG0KmE6HpcXa49B4jbCnkeyaV4'
    get_res(sec_id, max_cursor, info_list)
    print(info_list)